package cn.com.git.admin.dao.impl;

import cn.com.git.common.base.BaseDaoImpl;
import cn.com.git.admin.dao.SysResourceDAO;
import cn.com.git.admin.dao.mapper.SysResourceMapper;
import cn.com.git.admin.dao.model.SysResource;
import cn.com.git.admin.dao.model.SysResourceExample;
import cn.com.git.admin.service.query.SysResourceListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
/**
  * SysResourceDAO接口
  * @author zhangzhenfu
  * @Description
  * @date 2020/5/8
*/
@Repository
public class SysResourceDAOImpl extends BaseDaoImpl<SysResourceMapper,SysResource,SysResourceExample,SysResourceListQuery,Integer> implements SysResourceDAO {

    @Autowired
    private SysResourceMapper mapper;

    @Override
    protected SysResourceMapper getMapper() {
        return mapper;
    }

    @Override
    public SysResourceExample analysisLimit(SysResourceListQuery query) {
        SysResourceExample example = new SysResourceExample();
        SysResourceExample.Criteria c =  example.createCriteria();
        if(query.getLevel()!=null){
            c.andLevelEqualTo(query.getLevel());
        }
        if(query.getPid()!=null){
            c.andPidEqualTo(query.getPid());
        }
        if(query.getType()!=null){
            c.andTypeEqualTo(query.getType());
        }
        if(query.getIds()!=null){
            c.andIdIn(query.getIds());
        }
        //TODO
        return example;
    }

    @Override
    public SysResource selectById(Integer id) {
        return super.selectByPrimaryKey(id);
    }

    @Override
    public SysResource selectOne(SysResourceListQuery query) {
        SysResourceExample example = analysisLimit(query);
        //TODO
        List<SysResource> list =   Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
            if(list.size()>0){
                return list.get(0);
            }
        return null;
    }

    @Override
    public List<SysResource> selectList(SysResourceListQuery query) {
        SysResourceExample example = analysisLimit(query);
        return Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
    }

    @Override
    public int countLimit(SysResourceListQuery query) {
        SysResourceExample example = analysisLimit(query);
        return countByExample(example);
    }

    @Override
    public Integer insertOne(SysResource record) {
        super.insertSelective(record);
        return record.getId();
    }

    @Override
    public List<SysResource> insertList(List<SysResource> list) {
        Optional.ofNullable(list).orElse(Lists.newArrayList()).forEach(s->{
            super.insertSelective(s);
        });
        return list;
    }

    @Override
    public Integer updateOne(SysResource record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateLimit(SysResource record, SysResourceListQuery query) {
        SysResourceExample example = analysisLimit(query);
        return super.updateByExampleSelective(record,example);
    }

    @Override
    public Integer deleteOne(Integer id) {
        return super.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLimit(SysResourceListQuery query) {
        SysResourceExample example = analysisLimit(query);
        return super.deleteByExample(example);
    }

}